package jinyilw.swing.visuals;

import jinyilw.common.Operator;
import jinyilw.swing.Direction;
import jinyilw.swing.FontTools;
import jinyilw.swing.WindowTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.rlaf.ui.RLayoutAdapter;
import jinyilw.swing.visuals.Icomp.IButton;
import jinyilw.swing.visuals.Icomp.IRichTextLabel;
import jinyilw.swing.visuals.Jwindow.TransWindow;

import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class PopWindow extends TransWindow // 弹出窗
{
	private static final long serialVersionUID = -1620527224948235771L;
	static Color buttonColor = new Color(153, 255, 255);
	private IRichTextLabel infoLabel;
	private volatile ArrayList<Operator<?>> chooseOperators;
	private volatile ArrayList<Operator<?>> cancelOperators;
	private IButton chooseButton;
	private IButton cancelButton;
	private Container content;
	private boolean clickDispose = true;

	// private IString infoText, chooseText;

	public PopWindow()
	{
		initGUI();
	}

	public PopWindow(String infoText)
	{
		initGUI();
		infoLabel.setText(infoText);
	}

	public PopWindow(String infoText, String chooseText)
	{
		initGUI();
		infoLabel.setText(infoText);
		setChooseText(chooseText);
	}

	public PopWindow(String infoText, String chooseText, String cancelText)
	{
		initGUI();
		infoLabel.setText(infoText);
		setChooseText(chooseText);
		setCancelText(cancelText);
	}

	public PopWindow(ICSetting infoText)
	{
		initGUI();
		infoLabel.setText(infoText);
	}

	public PopWindow(ICSetting infoText, ICSetting chooseText)
	{
		initGUI();
		infoLabel.setText(infoText);
		setChooseText(chooseText);
	}

	private synchronized void createCancelOperaters()
	{
		if (cancelOperators == null)
		{
			cancelOperators = new ArrayList<>();
			getCancelButton().addActionListener(e -> {
				for (Operator<?> operator : cancelOperators)
					operator.run();
			});
		}
	}

	private synchronized void createChooseOperators()
	{
		if (chooseOperators == null)
		{
			chooseOperators = new ArrayList<>();
			getChooseButton().addActionListener(e -> {
				for (Operator<?> operator : chooseOperators)
					operator.run();
			});
		}
	}

	public void setChooseText(ICSetting chooseText)
	{
		getChooseButton().setText(chooseText);
	}

	public void setCancelText(ICSetting cancelText)
	{
		getCancelButton().setText(cancelText);
	}

	private void initGUI()
	{
		content = getContentPane();
		content.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (clickDispose)
					dispose();
			}
		});
		infoLabel = new IRichTextLabel();
		infoLabel.setForeground(Color.CYAN);
		infoLabel.setFont(FontTools.getLargeFont());
		content.add(infoLabel);
		setSize(220, 150);
		content.setLayout(new PopLayout());
	}

	public void showPopWindow()
	{
		WindowTools.showWindowFromSystemIcon(this);
	}

	public void hidePopWindow()
	{
		WindowTools.hideWindowToBorderInThread(this, Direction.DOWN);
	}

	public void setInfoText(ICSetting iText)
	{
		infoLabel.setText(iText);
	}

	public void setInfoText(String text)
	{
		infoLabel.setText(text);
	}

	public IButton getChooseButton()
	{
		if (chooseButton == null)
		{
			chooseButton = new IButton();
			chooseButton.setSize(90, 30);
			chooseButton.setBackground(buttonColor);
			chooseButton.setForeground(Color.darkGray);
			chooseButton.setDefaultAlpha(0.8f);
			chooseButton.setHoverAlpha(1f);
			content.add(chooseButton);
		}
		return chooseButton;
	}

	public IButton getCancelButton()
	{
		if (cancelButton == null)
		{
			cancelButton = new IButton();
			cancelButton.setSize(90, 30);
			content.add(cancelButton);
		}
		return cancelButton;
	}

	public void setChooseText(String text)
	{
		getChooseButton().setText(text);
	}

	public void setCancelText(String text)
	{
		getCancelButton().setText(text);
	}

	public void addChooseOperator(Operator<?> operator)
	{
		if (chooseOperators == null)
			createChooseOperators();
		chooseOperators.add(operator);
	}

	public void addCancelOperator(Operator<?> operator)
	{
		if (cancelOperators == null)
			createCancelOperaters();
		cancelOperators.add(operator);
	}

	public void setClickDispose(boolean b)
	{
		clickDispose = b;
	}

	@Override
	public Rectangle calculateWindowRect()
	{
		return WindowTools.calculateFromSystemWindowRect(this);
	}

	class PopLayout extends RLayoutAdapter
	{
		@Override
		public void layoutContainer(Container parent)
		{
			if (infoLabel != null)
				infoLabel.setLocation((getWidth() - infoLabel.getWidth()) / 2,
						25);
			if (cancelButton == null && chooseButton != null)
				chooseButton.setLocation(
						(getWidth() - chooseButton.getWidth()) / 2, 80);
			else if (cancelButton != null && chooseButton == null)
				cancelButton.setLocation(
						(getWidth() - cancelButton.getWidth()) / 2, 80);
			else if (cancelButton != null)
			{
				int w1 = chooseButton.getWidth(), w2 = cancelButton.getWidth();
				int x = (getWidth() - w1 - w2) / 2 - 10;
				cancelButton.setLocation(x, 80);
				chooseButton.setLocation(x + w2 + 20, 80);
			}
		}
	}
}
